home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 151 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  75 lines

  1. Path: news.cs.hope.edu!vnopstal
  2. From: vnopstal@cs.hope.edu (Michael Van Opstall)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to make a Complex class
  5. Date: 2 Jan 1996 20:12:01 GMT
  6. Organization: Hope College
  7. Message-ID: <4cc3ih$9if@news.cs.hope.edu>
  8. References: <4c8kdm$p0q@news.csie.nctu.edu.tw> <4c984a$99t@atlas.axiom.net> <4c9vvd$rjh@news.csie.nctu.edu.tw>
  9. NNTP-Posting-Host: smaug.cs.hope.edu
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. NortonNg (jkng@csie.nctu.edu.tw) wrote:
  13. : Dave Nulton (dnult@axiom.net) ┤ú¿∞:
  14. : : I've just been working with complex classes myself in C++.  The 
  15. : : class is in there.  There is already a library installed in my 
  16. : : compiler - use it by including <complex.h>.  It defines a 
  17. : : structure you use called complex.  Define a number something like 
  18. : : this.
  19. : : complex(double r, double i);
  20. : : As far as your format (typical mathmatical format) you'll have to 
  21. : : format you output and parse you input to make it work properly.
  22. : : The advantage to the complex library is that most all of the math 
  23. : : functions and stream operators have been overloaded to handle the 
  24. : : complex math.  In addition there are several standard functions.  
  25. : : The only thing not included is polar operators, but those are easy 
  26. : : to construct.
  27. : : Hope that helps
  28. : : -dnult
  29.  
  30. :     Thanks for you help, but I still have a problem can not be overcomed.
  31. : That is how to write to function which parse my input to make the complex
  32. : class work properly.  For instance, if I input (3+4.5i)*(2-8i)-(5i)
  33. : the function must parse it and then do the correct answer.
  34. :     I don't know how to make it, hope you can make it. Thanks....
  35.  
  36.  
  37. If you know how many complex number operations are going to take place,
  38. you can simply parse the input using cin like this:
  39.  
  40. char paren, op1, op2, op3, ichar;
  41. double r1, r2, i1, i2;
  42.  
  43. cin >>paren>>r1>>op1>>i1>>ichar>>paren>>op2>>paren>>r2>>op3>>i2>>ichar>>paren;
  44.  
  45. if (op1=='-') i1=-i1;
  46. if (op3=='+') i2=-i2;
  47.  
  48. Complex c1(r1,i1);
  49. Complex c2(r2,i2);
  50. Complex c3;
  51.  
  52. switch(op2) {
  53.    case '+':
  54.    c3=c1+c2;
  55.    break;
  56.    case '-':
  57.    c3=c1-c2;
  58.    break;
  59.    case '*':
  60.    c3=c1*c2;
  61.    break;
  62.    case '/':
  63.    c3=c1/c2;
  64.    break;
  65. }
  66.  
  67. assuming you have overloaded the ops for complex nums. Otherwise, parsing of
  68. mathematical input can be achieved using what is known as a finite state
  69. automaton (I know little about this, about 1 day in my c++ class) which would
  70. also effectively strip away white space.
  71.  
  72. --
  73. Michael A. Van Opstall  --  vnopstal@cs.hope.edu
  74. http://www.cs.hope.edu/~vnopstal/deal.html - try it
  75.